package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.*;
import com.wanmait.employment.mapper.*;
import com.wanmait.employment.service.UserinfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Create By pc on 2023/5/29.
 * 用户注册表
 */
@Service
public class UserinfoServiceImpl implements UserinfoService  {
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private NationMapper nationMapper;
    @Resource
    private MaritalStatusMapper maritalStatusMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private ExpectedSalaryMapper expectedSalaryMapper;
    @Resource
    private EntryTimeMapper entryTimeMapper;
    @Resource
    private StateMapper stateMapper;
    @Resource
    private RecruitmentEducationMapper recruitmentEducationMapper;
    @Resource
    private  WorkExperienceMapper workExperienceMapper;
    @Resource
    private NatureWorkMapper natureWorkMapper;
    @Resource
    private JobExperienceMapper jobExperienceMapper;
    @Resource
    private ProjectExperienceMapper projectExperienceMapper;
    @Resource
    private EducationalExperienceMapper educationalExperienceMapper;

    @Value("${hash.algorithmName}")
    private String algorithmName;
    @Value("${hash.iterations}")
    private Integer iterations;

    @Override
    public PageInfo<Userinfo> findAll(String name, int pageNum, int pageSize)  {
        PageHelper.startPage(1,10);
//         String title=null;
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",name).or().like("name",name).or().like("tel",name);
//
        List<Userinfo> userinfoList = userinfoMapper.selectList(queryWrapper);

        PageInfo<Userinfo> userinfoPageInfo= new PageInfo<>(userinfoList);
        userinfoPageInfo.getList().forEach(userinfo -> {
            //区
            QueryWrapper<Address>  addressQueryWrapper=new QueryWrapper<>();
            addressQueryWrapper.eq("id",userinfo.getAddressId());
            userinfo.setAddress(addressMapper.selectOne(addressQueryWrapper));
          //市
            QueryWrapper<Address>  cityQueryWrapper=new QueryWrapper<>();
            cityQueryWrapper.eq("id",userinfo.getAddress().getParentId());
            userinfo.setCity(addressMapper.selectOne(cityQueryWrapper));

            //省
            QueryWrapper<Address>  provinceQueryWrapper=new QueryWrapper<>();
            provinceQueryWrapper.eq("id",userinfo.getCity().getParentId());
            userinfo.setProvince(addressMapper.selectOne(provinceQueryWrapper));
            //民族
            QueryWrapper<Nation> nationQueryWrapper=new QueryWrapper<>();
            nationQueryWrapper.eq("id",userinfo.getNationId());
            Nation nation=nationMapper.selectOne(nationQueryWrapper);
            userinfo.setNation(nation);
            //婚姻状态
            QueryWrapper<MaritalStatus> maritalStatusQueryWrapper=new QueryWrapper<>();
            maritalStatusQueryWrapper.eq("id",userinfo.getMaritalStatusId());
            MaritalStatus maritalStatus= maritalStatusMapper.selectOne(maritalStatusQueryWrapper);
            userinfo.setMaritalStatus(maritalStatus);
            //期望职位
            QueryWrapper<Position> positionQueryWrapper=new QueryWrapper<>();
            positionQueryWrapper.eq("id",userinfo.getPositionId());
            Position position=positionMapper.selectOne(positionQueryWrapper);
            userinfo.setPosition(position);
            //期望薪资
            QueryWrapper<ExpectedSalary> expectedSalaryQueryWrapper = new QueryWrapper<>();
            expectedSalaryQueryWrapper.eq("id",userinfo.getExpectedSalaryId());
            ExpectedSalary expectedSalary=expectedSalaryMapper.selectOne(expectedSalaryQueryWrapper);
            userinfo.setExpectedSalary(expectedSalary);
            //入职时间
            QueryWrapper<EntryTime> entryTimeQueryWrapper= new QueryWrapper<>();
            entryTimeQueryWrapper.eq("id",userinfo.getEntryTime());
            EntryTime entryTime =entryTimeMapper.selectOne(entryTimeQueryWrapper);
            userinfo.setEntryTime(entryTime);
            //求职状态
            QueryWrapper<State> stateQueryWrapper=new QueryWrapper<>();
            stateQueryWrapper.eq("id",userinfo.getStateId());
            State state=stateMapper.selectOne(stateQueryWrapper);
            userinfo.setState(state);
            System.out.println(state.getTitle());
        });


        return userinfoPageInfo;
    }



    @Override
    public Integer delete(Integer id) {
    return userinfoMapper.deleteById(id);
    }

    @Override
    public Integer updata(Userinfo userinfo) {
      return  userinfoMapper.updateById(userinfo);
    }
    @Override
    public Integer resetpasswords(Integer id,String userpass) {
        return userinfoMapper.resetpasswords(id,userpass);
    }

    @Override
    public Integer resetpic(Integer id, String face) {
        return userinfoMapper.resetpic(id,face);
    }
    @Override
    public Userinfo login(Userinfo userinfo) {
        if(StringUtils.isBlank(userinfo.getTel())|| StringUtils.isBlank(userinfo.getUserpass())){
            return null;
        }
//        SimpleHash simpleHash=new SimpleHash()
        LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userinfo::getTel,userinfo.getTel());
        queryWrapper.eq(Userinfo::getUserpass,userinfo.getUserpass());
//        查询用户学历
        Userinfo userinfo1=userinfoMapper.selectOne(queryWrapper);
        QueryWrapper<RecruitmentEducation> recruitmentEducationQueryWrapper = new QueryWrapper<>();
        recruitmentEducationQueryWrapper.eq("id", userinfo1.getRecruitmentId());
        RecruitmentEducation scale=recruitmentEducationMapper.selectOne(recruitmentEducationQueryWrapper);
//        查询用户工作经验
        userinfo1.setRecruitmentEducation(scale);
        QueryWrapper<WorkExperience>workExperienceQueryWrapper=new QueryWrapper<>();
        workExperienceQueryWrapper.eq("id",userinfo1.getWorkexperienceId());

        WorkExperience workExperience=workExperienceMapper.selectOne(workExperienceQueryWrapper);
        userinfo1.setWorkExperience(workExperience);
        //查询用户所在地
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("id",userinfo1.getAddressId());
        Address address=addressMapper.selectOne(addressQueryWrapper);
        userinfo1.setAddress(address);
        //查询用户期望薪资
        QueryWrapper<ExpectedSalary>expectedSalaryQueryWrapper=new QueryWrapper<>();
        expectedSalaryQueryWrapper.eq("id",userinfo1.getExpectedSalaryId());
        ExpectedSalary expectedSalary=expectedSalaryMapper.selectOne(expectedSalaryQueryWrapper);
        userinfo1.setExpectedSalary(expectedSalary);

        QueryWrapper<NatureWork>natureWorkQueryWrapper=new QueryWrapper<>();
        natureWorkQueryWrapper.eq("id",userinfo1.getNatureWorkId());
        NatureWork natureWork=natureWorkMapper.selectOne(natureWorkQueryWrapper);
        userinfo1.setNatureWork(natureWork);

        QueryWrapper<EntryTime>entryTimeQueryWrapper=new QueryWrapper<>();
        entryTimeQueryWrapper.eq("id",userinfo1.getEntryTimeId());
        EntryTime entryTime=entryTimeMapper.selectOne(entryTimeQueryWrapper);
        userinfo1.setEntryTime(entryTime);
        QueryWrapper<Position>positionQueryWrapper=new QueryWrapper<>();
        positionQueryWrapper.eq("id",userinfo1.getPositionId());
        Position position=positionMapper.selectOne(positionQueryWrapper);
        userinfo1.setPosition(position);

        QueryWrapper<JobExperience> queryWrapper11 = new QueryWrapper<>();
        queryWrapper11.eq("userinfo_id",userinfo1.getId());
        List<JobExperience>jobExperienceList= jobExperienceMapper.selectList(queryWrapper11) ;
        userinfo1.setJobExperienceList(jobExperienceList);
        //查询项目经验
        QueryWrapper<ProjectExperience> projectExperienceQueryWrapper=new QueryWrapper<>();
        projectExperienceQueryWrapper.eq("userinfo_id",userinfo1.getId());
        List<ProjectExperience> projectExperienceList=projectExperienceMapper.selectList(projectExperienceQueryWrapper);
        userinfo1.setProjectExperienceList(projectExperienceList);

        //查询学历
        QueryWrapper<EducationalExperience>educationalExperienceQueryWrapper=new QueryWrapper<>();
        educationalExperienceQueryWrapper.eq("userinfo_id",userinfo1.getId());
        List<EducationalExperience> educationalExperienceList=educationalExperienceMapper.selectList(educationalExperienceQueryWrapper);
        userinfo1.setEducationalExperienceList(educationalExperienceList);

        return userinfo1;
    }

    @Override
    public Integer register(Userinfo userinfo) {
        return userinfoMapper.insert(userinfo);
    }

    @Override
    public List<RecruitmentEducation> relistAll() {
        List<RecruitmentEducation>list=recruitmentEducationMapper.selectList(null);
        return list;
    }

    @Override
    public List<WorkExperience> welistAll() {
        List<WorkExperience>list=workExperienceMapper.selectList(null);
        return list;
    }

    @Override
    public Integer userdate(Userinfo userinfo) {
        return  userinfoMapper.updateById(userinfo);
    }

    @Override
    public List<NatureWork> nwlistAll() {
        List<NatureWork>list=natureWorkMapper.selectList(null);
        return list;
    }
    @Override
    public List<EntryTime> etlistAll() {
        List<EntryTime>list=entryTimeMapper.selectList(null);
        return list;
    }
    @Override
    public List<ExpectedSalary> eslistAll() {
        List<ExpectedSalary>list=expectedSalaryMapper.selectList(null);
        return list;
    }

    @Override
    public List<Position> ptlistAll() {
        List<Position>list=positionMapper.selectList(null);
        return list;
    }

    @Override
    public Integer updatajob(JobExperience jobExperience) {
        return jobExperienceMapper.updateById(jobExperience);
    }

    @Override
    public Integer Addjob(JobExperience jobExperience) {
        return jobExperienceMapper.insert(jobExperience);
    }

    @Override
    public Integer delete(JobExperience jobExperience) {
        return jobExperienceMapper.deleteById(jobExperience);
    }

    @Override
    public List<JobExperience> Listjob(Integer id) {
        QueryWrapper<JobExperience> queryWrapper11 = new QueryWrapper<>();
        queryWrapper11.eq("userinfo_id",id).eq("enable",true);
        List<JobExperience>jobExperienceList= jobExperienceMapper.selectList(queryWrapper11) ;
        return jobExperienceList;
    }

    @Override
    public Userinfo ListAll(Integer id) {
//        SimpleHash simpleHash=new SimpleHash()
            LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Userinfo::getId,id);
//        查询用户学历
            Userinfo userinfo1=userinfoMapper.selectOne(queryWrapper);
            QueryWrapper<RecruitmentEducation> recruitmentEducationQueryWrapper = new QueryWrapper<>();
            recruitmentEducationQueryWrapper.eq("id", userinfo1.getRecruitmentId());
            RecruitmentEducation scale=recruitmentEducationMapper.selectOne(recruitmentEducationQueryWrapper);
//        查询用户工作经验
            userinfo1.setRecruitmentEducation(scale);
            QueryWrapper<WorkExperience>workExperienceQueryWrapper=new QueryWrapper<>();
            workExperienceQueryWrapper.eq("id",userinfo1.getWorkexperienceId());

            WorkExperience workExperience=workExperienceMapper.selectOne(workExperienceQueryWrapper);
            userinfo1.setWorkExperience(workExperience);
            //查询用户所在地
            QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
            addressQueryWrapper.eq("id",userinfo1.getAddressId());
            Address address=addressMapper.selectOne(addressQueryWrapper);
            userinfo1.setAddress(address);
            //查询用户期望薪资
            QueryWrapper<ExpectedSalary>expectedSalaryQueryWrapper=new QueryWrapper<>();
            expectedSalaryQueryWrapper.eq("id",userinfo1.getExpectedSalaryId());
            ExpectedSalary expectedSalary=expectedSalaryMapper.selectOne(expectedSalaryQueryWrapper);
            userinfo1.setExpectedSalary(expectedSalary);

            QueryWrapper<NatureWork>natureWorkQueryWrapper=new QueryWrapper<>();
            natureWorkQueryWrapper.eq("id",userinfo1.getNatureWorkId());
            NatureWork natureWork=natureWorkMapper.selectOne(natureWorkQueryWrapper);
            userinfo1.setNatureWork(natureWork);

            QueryWrapper<EntryTime>entryTimeQueryWrapper=new QueryWrapper<>();
            entryTimeQueryWrapper.eq("id",userinfo1.getEntryTimeId());
            EntryTime entryTime=entryTimeMapper.selectOne(entryTimeQueryWrapper);
            userinfo1.setEntryTime(entryTime);
            QueryWrapper<Position>positionQueryWrapper=new QueryWrapper<>();
            positionQueryWrapper.eq("id",userinfo1.getPositionId());
            Position position=positionMapper.selectOne(positionQueryWrapper);
            userinfo1.setPosition(position);

            QueryWrapper<JobExperience> queryWrapper11 = new QueryWrapper<>();
            queryWrapper11.eq("userinfo_id",userinfo1.getId());
            List<JobExperience>jobExperienceList= jobExperienceMapper.selectList(queryWrapper11) ;
            userinfo1.setJobExperienceList(jobExperienceList);

            QueryWrapper<ProjectExperience> projectExperienceQueryWrapper=new QueryWrapper<>();
            projectExperienceQueryWrapper.eq("userinfo_id",userinfo1.getId());
            List<ProjectExperience> projectExperienceList=projectExperienceMapper.selectList(projectExperienceQueryWrapper);
            userinfo1.setProjectExperienceList(projectExperienceList);
            return userinfo1;

    }

    @Override
    public Integer saveOrUpdate(ProjectExperience projectExperience) {
        return projectExperienceMapper.updateById(projectExperience);
    }

    @Override
    public Integer addpe(ProjectExperience projectExperience) {
        return projectExperienceMapper.insert(projectExperience);
    }

    @Override
    public List<EducationalExperience> edlist(Integer id) {
        QueryWrapper<EducationalExperience> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userinfo_id",id);
        List<EducationalExperience> experienceList= educationalExperienceMapper.selectList(queryWrapper);
        return experienceList;
    }

    @Override
    public Integer edsaveOrUpdate(EducationalExperience educationalExperience) {
        return educationalExperienceMapper.updateById(educationalExperience);
    }

    @Override
    public Integer inserted(EducationalExperience educationalExperience) {
        return educationalExperienceMapper.insert(educationalExperience);
    }

    @Override
    public Integer deleteed(Integer id) {
        return educationalExperienceMapper.deleteById(id);
    }

    @Override
    public Integer deletepe(Integer id) {
        return projectExperienceMapper.deleteById(id);
    }

    @Override
    public Userinfo SelectOne(Integer userinfoId){
        QueryWrapper<Userinfo> userinfoQueryWrapper=new QueryWrapper<>();
        userinfoQueryWrapper.eq("id",userinfoId);
        Userinfo userinfo=userinfoMapper.selectOne(userinfoQueryWrapper);

        LambdaQueryWrapper<Address> addressLambdaQueryWrapper=new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getId,userinfo.getAddressId());
        Address address=addressMapper.selectOne(addressLambdaQueryWrapper);
        userinfo.setAddress(address);

        LambdaQueryWrapper<Address> cityQueryWrapper=new LambdaQueryWrapper<>();
        cityQueryWrapper.eq(Address::getId,address.getParentId());
        Address city=addressMapper.selectOne(cityQueryWrapper);
        //        查询省
        LambdaQueryWrapper<Address> provinceQueryWrapper=new LambdaQueryWrapper<>();
        provinceQueryWrapper.eq(Address::getId,city.getParentId());
        userinfo.setProvince(addressMapper.selectOne(provinceQueryWrapper));
        //  民族
        QueryWrapper<Nation> nationQueryWrapper = new QueryWrapper<>();
        nationQueryWrapper.eq("id", userinfo.getNationId());
        Nation nation = nationMapper.selectOne(nationQueryWrapper);
        userinfo.setNation(nation);
        //  婚姻
        QueryWrapper<MaritalStatus> maritalStatusQueryWrapper = new QueryWrapper<>();
        maritalStatusQueryWrapper.eq("id", userinfo.getMaritalStatusId());
        MaritalStatus maritalStatus = maritalStatusMapper.selectOne(maritalStatusQueryWrapper);
        userinfo.setMaritalStatus(maritalStatus);

        QueryWrapper<State> stateQueryWrapper = new QueryWrapper<>();
        stateQueryWrapper.eq("id", userinfo.getStateId());
        State state = stateMapper.selectOne(stateQueryWrapper);
        userinfo.setState(state);

        QueryWrapper<Position> positionQueryWrapper = new QueryWrapper<>();
        positionQueryWrapper.eq("id", userinfo.getPositionId());
        Position position = positionMapper.selectOne(positionQueryWrapper);
        userinfo.setPosition(position);

        QueryWrapper<ExpectedSalary> expectedSalaryQueryWrapper = new QueryWrapper<>();
        expectedSalaryQueryWrapper.eq("id", userinfo.getExpectedSalaryId());
        ExpectedSalary expectedSalary = expectedSalaryMapper.selectOne(expectedSalaryQueryWrapper);
        userinfo.setExpectedSalary(expectedSalary);

        QueryWrapper<EntryTime> entryTimeQueryWrapper = new QueryWrapper<>();
        entryTimeQueryWrapper.eq("id", userinfo.getEntryTimeId());
        EntryTime entryTime = entryTimeMapper.selectOne(entryTimeQueryWrapper);
        userinfo.setEntryTime(entryTime);

        QueryWrapper<RecruitmentEducation> recruitmentEducationQueryWrapper = new QueryWrapper<>();
        recruitmentEducationQueryWrapper.eq("id", userinfo.getRecruitmentId());
        RecruitmentEducation recruitmentEducation = recruitmentEducationMapper.selectOne(recruitmentEducationQueryWrapper);
        userinfo.setRecruitmentEducation(recruitmentEducation);

        QueryWrapper<WorkExperience> workExperienceQueryWrapper = new QueryWrapper<>();
        workExperienceQueryWrapper.eq("id", userinfo.getWorkexperienceId());
        WorkExperience workExperience = workExperienceMapper.selectOne(workExperienceQueryWrapper);
        userinfo.setWorkExperience(workExperience);

        QueryWrapper<NatureWork> natureWorkQueryWrapper = new QueryWrapper<>();
        natureWorkQueryWrapper.eq("id", userinfo.getNatureWorkId());
        NatureWork natureWork = natureWorkMapper.selectOne(natureWorkQueryWrapper);
        userinfo.setNatureWork(natureWork);
        return userinfo;
    }


}
